package com.exrobot.module.rbt.controller.admin.canteenmenu;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.canteenmenu.vo.*;
import com.exrobot.module.rbt.dal.dataobject.canteenmenu.CanteenMenuDO;
import com.exrobot.module.rbt.service.canteenmenu.CanteenMenuService;

@Tag(name = "管理后台 - 食堂菜单")
@RestController
@RequestMapping("/rbt/canteen-menu")
@Validated
public class CanteenMenuController {

    @Resource
    private CanteenMenuService canteenMenuService;

    @PostMapping("/create")
    @Operation(summary = "创建食堂菜单")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:create')")
    public CommonResult<Long> createCanteenMenu(@Valid @RequestBody CanteenMenuSaveReqVO createReqVO) {
        return success(canteenMenuService.createCanteenMenu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食堂菜单")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:update')")
    public CommonResult<Boolean> updateCanteenMenu(@Valid @RequestBody CanteenMenuSaveReqVO updateReqVO) {
        canteenMenuService.updateCanteenMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食堂菜单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:delete')")
    public CommonResult<Boolean> deleteCanteenMenu(@RequestParam("id") Long id) {
        canteenMenuService.deleteCanteenMenu(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除食堂菜单")
                @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:delete')")
    public CommonResult<Boolean> deleteCanteenMenuList(@RequestParam("ids") List<Long> ids) {
        canteenMenuService.deleteCanteenMenuListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食堂菜单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:query')")
    public CommonResult<CanteenMenuRespVO> getCanteenMenu(@RequestParam("id") Long id) {
        CanteenMenuDO canteenMenu = canteenMenuService.getCanteenMenu(id);
        return success(BeanUtils.toBean(canteenMenu, CanteenMenuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食堂菜单分页")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:query')")
    public CommonResult<PageResult<CanteenMenuRespVO>> getCanteenMenuPage(@Valid CanteenMenuPageReqVO pageReqVO) {
        PageResult<CanteenMenuDO> pageResult = canteenMenuService.getCanteenMenuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CanteenMenuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出食堂菜单 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-menu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCanteenMenuExcel(@Valid CanteenMenuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CanteenMenuDO> list = canteenMenuService.getCanteenMenuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食堂菜单.xls", "数据", CanteenMenuRespVO.class,
                        BeanUtils.toBean(list, CanteenMenuRespVO.class));
    }

}